﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using RssWindowsPhone.ServiceReference;

namespace RssWindowsPhone
{
    public partial class ModifyRss : PhoneApplicationPage
    {
        String user;
        String password;
        String id;
        String title;
        String link;

        public ModifyRss()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            NavigationContext.QueryString.TryGetValue("User", out this.user);
            NavigationContext.QueryString.TryGetValue("Password", out this.password);
            NavigationContext.QueryString.TryGetValue("FeedID", out this.id);
            NavigationContext.QueryString.TryGetValue("FeedTitle", out this.title);
            NavigationContext.QueryString.TryGetValue("FeedLink", out this.link);

            this.FeedEditTitleTxtBox.Text = this.title;
            this.FeedEditUrlTxtBox.Text = this.link;
        }

        private void FeedEditBtn_Click(object sender, RoutedEventArgs e)
        {
            ServiceRssClient client = new ServiceRssClient();

            EditFlux toModify = new EditFlux()
            {
                ID = System.Convert.ToInt32(this.id),
                Title = this.FeedEditTitleTxtBox.Text,
                Link = this.FeedEditUrlTxtBox.Text,
            };

            LoginModel log = new LoginModel()
            {
                Password = this.password,
                UserName = this.user,
                RememberMe = false
            };

            client.EditRssAsync(log, toModify);
            client.EditRssCompleted += new EventHandler<EditRssCompletedEventArgs>(client_EditRssCompleted);
        }

        void client_EditRssCompleted(object sender, EditRssCompletedEventArgs e)
        {
            this.FeedEditErrorBox.Text = "";
            FluxStatus edited = e.Result;

            if (edited == FluxStatus.Success)
                NavigationService.Navigate(new Uri("/CoreApp.xaml?User=" + this.user + "&Password=" + this.password, UriKind.Relative));
            else
                this.FeedEditErrorBox.Text = edited.ToString();
        }
    }
}